# __author:wanghao
# data:2022/6/7
import time

from case.base.basepage import BasePage
from case.base import route


class HomePage(BasePage):
    """小程序首页公共方法"""

    locators = {
        "BASE_ELEMENT": "view",
        "BASE_BANNER": "scroll-view[capture-bind:touchend='handleAccessRecordTouchEnd'][class='access-record']",
        "BASE_GOODS_IMAGE": "view[catch:tap='gotoGoodsDetail'][class='goods']",
        "BASE_STORE_IMAGE": "view[catch:tap='gotoShopHomePage'][class='item']"
    }

    """
    校验页面路径
    """

    def check_page_path(self, route_path):
        self.mini.assertEqual(self.current_path(), route_path, "当前页面路径不符合预期")
        return self

    """
    校验页面的基本元素
    """

    def check_homepage_base_element(self):
        # 校验页面是否包含view元素
        self.mini.assertTrue(self.mini.page.element_is_exists(HomePage.locators['BASE_ELEMENT']))
        # 校验页面banner位置
        self.mini.assertTrue(self.mini.page.element_is_exists(HomePage.locators['BASE_BANNER']))
        return self

    """
    校验页面的推荐商品
    """

    def check_homepage_goods_element(self):
        # 校验页面是否包含推荐商品
        self.mini.assertTrue(self.mini.page.element_is_exists(HomePage.locators['BASE_GOODS_IMAGE']))
        # 判断页面元素是否正确
        result = self.mini.page.wait_for("single-live[is='components/single-live-v3/index'][data-index='0'] > view",3)  # 等待页面渲染完成
        if result:
        # 获取商品名称
            element = self.mini.page.get_element("single-live[is='components/single-live-v3/index'][data-index='0']").get_element("view[catch:tap='gotoGoodsDetail'][class='goods']")
            goods_name = element.inner_text.split('\n')[0]
            element.click()
            time.sleep(1)
            detail_name_result = self.mini.page.wait_for("view[class='title van-multi-ellipsis--l2']")
            if detail_name_result:
                detail_goods_name = self.mini.page.get_element("view[class='title van-multi-ellipsis--l2']").inner_text
                self.mini.assertIn(goods_name,detail_goods_name,"商品标题不符合预期")

    """
    校验页面的直播
    """

    def check_homepage_living_element(self):
        # 校验页面是否包含推荐商品
        self.mini.assertTrue(self.mini.page.element_is_exists(HomePage.locators['BASE_GOODS_IMAGE']))
        # 判断页面元素是否正确
        living_index = -1
        if self.mini.page.wait_for("single-live[is='components/single-live-v3/index'][data-index='1'] >>> live-status", 3):
            living_index = 1
        elif self.mini.page.wait_for("single-live[is='components/single-live-v3/index'][data-index='2'] >>> live-status", 3):
            living_index = 2
        else:
            living_index = -1
        living_element = self.mini.page.get_element("single-live[is='components/single-live-v3/index'][data-index='%d']" % living_index).get_element("live-status[is='components/live-status-v2/index']")
        living_element.click()
        if living_index > 0:
            # time.sleep(500)
            self.mini.page.element_is_exists("player-icons[is='packages/main/live-room/components/player-icons/index'][bind:playchannelchange='handlePlayChannelChange'] >>> view", 10)
            goods_package_element = self.mini.page.get_element("player-icons[is='packages/main/live-room/components/player-icons/index'][bind:playchannelchange='handlePlayChannelChange'] >>> view")
            goods_package_name = goods_package_element.inner_text.split('\n')[0]
            self.mini.assertEqual(goods_package_name, "商品", "进入直播间成功")

    """
    校验页面的小店入口
    """

    def check_homepage_store_element(self):
        # 校验页面是否包含view元素
        self.mini.assertTrue(self.mini.page.element_is_exists(HomePage.locators['BASE_ELEMENT']))
        # 校验页面小店入口
        self.mini.assertTrue(self.mini.page.element_is_exists(HomePage.locators['BASE_STORE_IMAGE']))
        return self

    """
    进入搜索页面
    """

    def into_search_page(self):
        # 校验页面是否包含推荐商品
        self.mini.assertTrue(self.mini.page.element_is_exists(HomePage.locators['BASE_GOODS_IMAGE']))
        self.mini.page.get_element("swiper-item.content-index--search-label").click()
        self.mini.page.wait_for("input[class='input']", 10)
        # 输入搜索关键字
        search_value = "月亮与六便士"
        search = self.mini.page.get_element("input[class='input']")
        search.input(search_value)
        search.trigger("confirm", {"value": search_value})
        self.mini.page.wait_for("view[class='goods-info__title van-multi-ellipsis--l2']",10)
        result_name = self.mini.page.get_element('view.item-index--title.van-multi-ellipsis--l2').inner_text
        assert search_value in result_name
